package com.feituan.manager.controller;

import com.feituan.core.pojo.entity.TypeCoupons;
import com.feituan.core.pojo.request.TypeCouponsPageReq;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.service.ITypeCouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/typeCoupons")
@Api(tags = "优惠卷类型与优惠卷数组关系管理")
public class TypeCouponsController extends BaseController{
    @Autowired
    private ITypeCouponsService typeCouponsService;


    @PostMapping("/save")
    @ApiOperation("id为空新增，id不为空修改")
    public Response<Boolean> save(@RequestBody TypeCoupons typeCoupons) {
        TypeCoupons coupon = new TypeCoupons();
        BeanUtils.copyProperties(typeCoupons,coupon);
        if(coupon.getId() == null){
            typeCoupons.setCreateTime(new Date());
            return response(typeCouponsService.save(typeCoupons)) ;
        }else{
            return response(typeCouponsService.updateById(typeCoupons));
        }
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除")
    public Response<Boolean> delete(Long id) {
        return response(typeCouponsService.removeById(id));
    }

    @PostMapping("/list_page")
    @ApiOperation("查询列表")
    public Response<PageData<TypeCoupons>> listPage(TypeCouponsPageReq typeCouponsPageReq) {
        setDefaultPageParameters(typeCouponsPageReq);
        return Response.success(typeCouponsService.queryPage(typeCouponsPageReq));
    }
}
